<?php

namespace App\Http\Controllers;

use App\Models\Store;
use App\Models\StorePayType;
use App\Util\Tool;
use Illuminate\Http\Request;
use App\Models\StoreCategory;
use App\Http\Controllers\Controller;

class StoreController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $param = $request->input() ?: [];
        if (empty($param['lng'])) {
            return $this->ResultJson(0, '经度不能为空!!', []);
        }
        if (empty($param['lat'])) {
            return $this->ResultJson(0, '纬度不能为空!!', []);
        }
        $course = (new Store())->getStoreList($param);
        return $this->ResultJson(1, '获取成功!', $course);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $res = $request->input() ?: [];
        if(isset($res['id']) && !empty($res['id'])){
            $param = true;
        }else{
            $param = $this->verify($res);
        }
        if ($param === true) {
            $result = (new Store())->addStore($res);
            if (is_array($result)) {
                return $this->ResultJson(1, '提交成功!', $result);
            } else {
                return $this->ResultJson(0, $result, []);
            }
        }
        return $this->ResultJson(0, $param, []);;
    }

    /****
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 数据验证
     */
    public function verify($res)
    {
        if (empty($res['user_id'])) {
            return '用户id不能为空!';
        }
        if (empty($res['phone']) || !isset($res['phone'])) {
            return '手机号不能为空!';
        }
        //判断短信验证码是否过期
        $code = Tool::GetSmsvalidator($res['phone']);
        if (empty($code)) {
            return '短信验证码已经过期';
        }
        if (!isset($res['code'])) {
            return '请输入验证码!';
        }
        //需要检查短信验证码是正确
        if (isset($res['code']) && $res['code'] != $code) {
            return $this->ResultJson(0, '短信验证码错误', []);
        }
        //更新验证码为验证过了
        $this->SetCode($res['phone'], $code);
        //短信认证设置失效时间
        Tool::UsetSmsCode($res['phone']);

//        if (empty($res['wechat']) || !isset($res['wechat'])) {
//            return '微信号不能为空!';
//        }
        if (!$this->isValidPhoneNumber($res['phone'])) {
            return '电话格式不正确!';
        }
        if (empty($res['shop_name'])) {
            return '店铺名称不能为空!';
        }
        if (empty($res['shop_type'])) {
            return '行业类型不能为空!';
        }
        if (empty($res['address'])) {
            return '详细地址不能为空!';
        }
        if (empty($res['lng'])) {
            return '经度不能为空!';
        }
        if (empty($res['lat'])) {
            return '纬度不能为空!';
        }
        if (empty($res['logo'])) {
            return '请上传店铺LOGO!';
        }
        return true;
    }


    /***
     * @param $number
     * @return false|int
     * 验证手机号
     */
    protected function isValidPhoneNumber($number) {
        return preg_match('/^1[3-9]\d{9}$/', $number);
    }



    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $res = $request->input();
        if (empty($res['id'])) {
            return $this->ResultJson(0, '商家id不能为空', []);
        }
        $res = (new Store())->getInfo($res['id']);
        if(!empty($res)){
            return $this->ResultJson(1, '获取成功', $res->toArray());
        }else{
            return $this->ResultJson(0, '暂无数据', []);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 商圈分类列表
     */
    public function storeCategory(Request $request)
    {
        $param = $request->input();
        $imgs = (new StoreCategory())->getstoreCategory($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取购买类型
     */
    public function getStorePayType(Request $request)
    {
        $param = $request->input();
        if(empty($param['cate_type'])){
            return $this->ResultJson(0, '分类ID不能为空!', []);
        }
        $imgs = (new StorePayType())->getStorePayType($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }






    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function storePay(Request $request)
    {
        $res = $request->input();
        if (empty($res['store_order_id'])) {
            return $this->ResultJson(0, '商圈订单id不能为空', []);
        }
        if (empty($res['type_id'])) {
            return $this->ResultJson(0, '购买类型不能为空', []);
        }
        if (empty($res['pay_type'])) {
            return $this->ResultJson(0, '支付类型不能为空', []);
        }
        $result = (new Store())->pay($res);
        if ($res['pay_type'] == 1) {  //微信支付
            if (is_array($result)) {
                return $this->ResultJson(1, "支付宝支付成功!", $result);
            }
        } elseif ($res['pay_type'] == 2) { //支付宝
            if ($result) {
                return $this->ResultJson(1, "支付宝支付成功!", ['str' => $result]);
            }
        }
        return $this->ResultJson(0, "支付失败!", []);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Store $store)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function destroy(Store $store)
    {
        //
    }
}
